package com.emr.web.controller.emr;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.emr.common.annotation.Log;
import com.emr.common.base.AjaxResult;
import com.emr.common.enums.BusinessType;
import com.emr.common.utils.ExcelUtil;
import com.emr.framework.web.page.TableDataInfo;
import com.emr.module.domain.Bed;
import com.emr.module.domain.Personal;
import com.emr.module.service.IBedService;
import com.emr.module.service.IPersonalService;
import com.emr.system.service.ISysUserService;
import com.emr.web.core.base.BaseController;

/**
 * 住院病例 信息操作处理
 * 
 * @author emr
 * @date 2019-02-20
 */
@Controller
@RequestMapping("/module/bed")
public class BedController extends BaseController
{
    private String prefix = "module/bed";
	
	@Autowired
	private IBedService bedService;
	@Autowired
	private IPersonalService ipersonalService ;
	@Autowired
	private ISysUserService isysUserService;
	
	
	@RequiresPermissions("module:bed:view")
	@GetMapping()
	public String bed()
	{
	    return prefix + "/bed";
	}
	
	/**
	 * 查询住院病例列表
	 */
	@RequiresPermissions("module:bed:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Bed bed)
	{
		startPage();
		if("".equals(bed.getPatientName())){
			bed.setPatientName(null);
		}
        List<Bed> list = bedService.selectBedList(bed);
		return getDataTable(list);
	}
	
	/**
	 * 新增住院病例
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap)
	{
		mmap.put("posts",ipersonalService.selectIcdAll());
		mmap.put("hushi", isysUserService.selectUserAll());
		mmap.put("yisheng", isysUserService.selectyishengAll());
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存住院病例
	 */
	@RequiresPermissions("module:bed:add")
	@Log(title = "住院病例", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Bed bed)
	{		
		return toAjax(bedService.insertBed(bed));
	}

	/**
	 * 修改住院病例
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		Bed beds = bedService.selectBedById(id);
		mmap.put("posts", ipersonalService.selectUPersonalByIds(beds.getPersonalId()));
		
		mmap.put("hushi", isysUserService.selecthushiAlls(beds.getPersonalHushiid()));
		mmap.put("yisheng", isysUserService.selectyishengAlls(beds.getPersonalYishengid()));
		
		Bed bed = bedService.selectBedById(id);
		mmap.put("bed", bed);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存住院病例
	 */
	@RequiresPermissions("module:bed:edit")
	@Log(title = "住院病例", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Bed bed)
	{		
		return toAjax(bedService.updateBed(bed));
	}
	
	/**
	 * 删除住院病例
	 */
	@RequiresPermissions("module:bed:remove")
	@Log(title = "住院病例", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(bedService.deleteBedByIds(ids));
	}
	/***
	 * 住院病历
	 * @param personal
	 * @return
	 */
	@Log(title = "个人病例", businessType = BusinessType.EXPORT)
    @RequiresPermissions("module:bed:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Bed bed)
    {
        List<Bed> list = bedService.selectBedList(bed);
        ExcelUtil<Bed> util = new ExcelUtil<Bed>(Bed.class);
        return util.exportExcel(list, "bed");
    }
}
